include('shared.lua')
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
AddCSLuaFile("cl_health.lua")

resource.AddFile("materials/redbar.vmt")
resource.AddFile("materials/redbar.vtf")

LastUseCall = {}

function ENT:Initialize()
	
	self:SetModel( self.Model )
	
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)

	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:EnableMotion(false)
	end	
	
	self:UpdateNW()
	
	self.NextHealthGive = CurTime()

end

function ENT:SetTeam( team )
	self.TEAM = team
end

function ENT:SetBuildState( state )
	self.BuildState = state
	
	self:StateChanged()
end

function ENT:StateChanged() // For override
end

function ENT:ArcSetHealth( health )
	self.HEALTH = math.min( health, self.MaxHealth )
end

function ENT:Think()
	
	if self:Building() then
		
		if CurTime() - self.SpawnTime > self.BuildTime then
			self:SetBuildState( false )
			self:ArcSetHealth( self:ArcHealth() + self.MaxHealth * 0.25 )
		end
		
		if self.ExtraHealth != true && CurTime() - self.SpawnTime > self.BuildTime / 2 then
			self:ArcSetHealth( self:ArcHealth() + self.MaxHealth * 0.25 )
			self.ExtraHealth = true
		end
		
		return
	end

end

function ENT:OnTakeDamage(dmg)

	local NewHealth = self:ArcHealth() - dmg:GetDamage()
	
	if NewHealth <= 0 then
		self:Remove()
	end
	
	self:ArcSetHealth( NewHealth )
	
end

function ENT:Use( ply )
 
	if (LastUseCall[ ply ] or 0) > CurTime() then
		return
	end
	
	LastUseCall[ ply ] = CurTime() + 0.5

end